#include "stdafx.h"
#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#include "ini.h"
#include "wx/utils.h"

/* Ini */
	
Ini::Ini(void)
{
		sl = new wxArrayString();
		ifl = new wxArrayString();
		efl = new wxArrayString();
		edl = new wxArrayString();
	sif = new wxString();
	sed = new wxString();
	sef = new wxString();
}

Ini::~Ini(void)
{
	delete sef;
	delete sed;
	delete sif;
		delete sl;
		delete ifl;
		delete efl;
		delete edl;
}

bool Ini::readFiles(wxCmdLineParser *clp)
{
	bool result = true;
	wxString fn;
	if( clp->Found(srcl, &fn) )
	{
		result = result && readFile(fn, sl);
	}
	if( clp->Found(ifilesl, &fn) )
	{
		result = result && readFile(fn, ifl);
	}
	if( clp->Found(efilesl, &fn) )
	{
		result = result && readFile(fn, efl);
	}
	if( clp->Found(edirsl, &fn) )
	{
		result = result && readFile(fn, edl);
	}
	return result;
}

bool Ini::readFile(const wxString &fn, wxArrayString * a)
{
	wxString str;
	if( !wxFileExists(fn) )
	{
		wxPrintf(_T("\n%s file not found %s"), err_str,  fn.c_str());
		return false;
	}
	wxTextFile *tf = new wxTextFile();
	try
	{
		a->Clear();
		tf->Open(fn);
		for( str = tf->GetFirstLine(); !tf->Eof(); str = tf->GetNextLine().Trim() )
		{
			if( str.Length() )
				a->Add(str);
		}
		str = tf->GetLastLine().Trim();
		if( str.Length() )
			a->Add(str);
		tf->Close();
		delete tf;
	}
	catch(...)
	{
		delete tf;
		return false;
	}
	return true;
}

wxString Ini::concatenate(wxArrayString *a)
{
	wxString result;
	for( int i = a->GetCount() - 1; i >= 0; i--)
		result += a->Item(i) + wxT("|");
	result = result.Mid(0, result.Length() - 1 );
	return result;
}

wxString Ini::conIFiles(void)
{
	return concatenate(ifl);
}

wxString Ini::conEFiles(void)
{
	return concatenate(efl);
}

wxString Ini::conEDirs(void)
{
	return concatenate(edl);
}


